﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceProcess;

namespace SpringNETAS.Monitor
{
    public partial class SpringNETASMonitor : Form
    {
        #region Constructor
        public SpringNETASMonitor()
        {
            InitializeComponent();

            //Menu Spring NET AS Monitor Events
            SpringNETASContextMenu.Items["tsSpringNETAS_Start"].Click += new EventHandler(tsSpringNETAS_Start_Click);
            SpringNETASContextMenu.Items["tsSpringNETAS_Pause"].Click += new EventHandler(tsSpringNETAS_Pause_Click);
            SpringNETASContextMenu.Items["tsSpringNETAS_Stop"].Click += new EventHandler(tsSpringNETAS_Stop_Click);
            SpringNETASContextMenu.Items["tsSpringNETAS_Exit"].Click += new EventHandler(tsSpringNETAS_Exit_Click);

            //keep the form hidden
            this.Hide();
        }
        #endregion

        #region Private Methods
        private void SetButtonStatus()
        {
            switch (SpringNETASServiceController.Status)
            {
                case ServiceControllerStatus.Running:
                    SpringNETASContextMenu.Items["tsSpringNETAS_Start"].Enabled = false;
                    SpringNETASContextMenu.Items["tsSpringNETAS_Pause"].Enabled = true;
                    SpringNETASContextMenu.Items["tsSpringNETAS_Stop"].Enabled = true;
                    break;
                case ServiceControllerStatus.Paused:
                    SpringNETASContextMenu.Items["tsSpringNETAS_Start"].Enabled = true;
                    SpringNETASContextMenu.Items["tsSpringNETAS_Pause"].Enabled = false;
                    SpringNETASContextMenu.Items["tsSpringNETAS_Stop"].Enabled = true;
                    break;
                case ServiceControllerStatus.Stopped:
                    SpringNETASContextMenu.Items["tsSpringNETAS_Start"].Enabled = true;
                    SpringNETASContextMenu.Items["tsSpringNETAS_Pause"].Enabled = false;
                    SpringNETASContextMenu.Items["tsSpringNETAS_Stop"].Enabled = false;
                    break;
            }
        }
        #endregion

        #region Private Form Events
        private void SpringNETASMonitor_Load(object sender, EventArgs e)
        {
            ServiceController[] AvailableServices = ServiceController.GetServices(".");

            foreach (ServiceController AvailableService in AvailableServices)
            {
                //Check the service name for Spring Net As
                if (AvailableService.ServiceName == SPRING_NET_AS_NAME)
                {
                    SpringNETASServiceController.ServiceName = SPRING_NET_AS_NAME;

                    SetButtonStatus();
                    return;
                }
            }

            MessageBox.Show("The Spring Net AS Service is not installed on this Machine", "Spring Net AS Service is not available",MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1 );
            this.Close();
        }

        private void SpringNETASWSNotifyIcon_Click(object sender, EventArgs e)
        {
            SpringNETASServiceController.Refresh();

            SetButtonStatus();
        }
        #endregion

        #region Menu Spring NET AS Monitor Events
        private void tsSpringNETAS_Start_Click(object sender, EventArgs e)
        {
            switch (SpringNETASServiceController.Status)
            {
                case ServiceControllerStatus.Paused :
                    SpringNETASServiceController.Continue();

                    break;
                case ServiceControllerStatus.Stopped :
                    //get an array of services this service depends upon, loop through 
                    //the array and prompt the user to start all required services.
                    ServiceController[] ParentServices = SpringNETASServiceController.ServicesDependedOn;

                    //if the length of the array is greater than or equal to 1.
                    if (ParentServices.Length >= 1)
                    {
                        foreach (ServiceController ParentService in ParentServices)
                        {
                            //make sure the parent service is running or at least paused.
                            if (ParentService.Status != ServiceControllerStatus.Running || ParentService.Status != ServiceControllerStatus.Paused)
                            {
                                if (MessageBox.Show("This service is required. Would you like to also start this service?\n" + ParentService.DisplayName, "Required Service", MessageBoxButtons.YesNo).ToString() == "Yes")
                                {
                                    //if the user chooses to start the service

                                    ParentService.Start();
                                    ParentService.WaitForStatus(ServiceControllerStatus.Running);
                                }
                                else
                                {
                                    //otherwise just return.
                                    return;
                                }
                            }
                        }
                    }

                    SpringNETASServiceController.Start();

                    break;
            }

            SpringNETASServiceController.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running);
            SetButtonStatus();
        }
        private void tsSpringNETAS_Pause_Click(object sender, EventArgs e)
        {
            //check to see if the service can be paused and continue
            if (SpringNETASServiceController.CanPauseAndContinue == true)
            {
                //check the status of the service
                if (SpringNETASServiceController.Status == ServiceControllerStatus.Running)
                {
                    SpringNETASServiceController.Pause();
                }

                SpringNETASServiceController.WaitForStatus(ServiceControllerStatus.Paused);
                SetButtonStatus();
            }
        }
        private void tsSpringNETAS_Stop_Click(object sender, EventArgs e)
        {
            //check to see if the service can be stopped.
            if (SpringNETASServiceController.CanStop == true)
            {

                //get an array of dependent services, loop through the array and 
                //prompt the user to stop all dependent services.
                ServiceController[] DependentServices = SpringNETASServiceController.DependentServices;

                //if the length of the array is greater than or equal to 1.
                if (DependentServices.Length >= 1)
                {
                    foreach (ServiceController DependentService in DependentServices)
                    {
                        //make sure the dependent service is not already stopped.
                        if (DependentService.Status != ServiceControllerStatus.Stopped)
                        {
                            if (MessageBox.Show("Would you like to also stop this dependent service?\n" + DependentService.DisplayName, "Dependent Service", MessageBoxButtons.YesNo).ToString() == "Yes")
                            {
                                // not checking at this point whether the dependent service can be stopped.
                                // developer may want to include this check to avoid exception.
                                DependentService.Stop();
                                DependentService.WaitForStatus(ServiceControllerStatus.Stopped);
                            }
                            else
                            {
                                return;
                            }
                        }
                    }
                }

                //check the status of the service
                if (SpringNETASServiceController.Status == ServiceControllerStatus.Running || SpringNETASServiceController.Status == ServiceControllerStatus.Paused)
                {
                    SpringNETASServiceController.Stop();
                }

                SpringNETASServiceController.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped);
                SetButtonStatus();
            }
        }

        private void tsSpringNETAS_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
    }    
}
